package com.micro.modual.service.impl;

import com.micro.common.MicroConstant;
import com.micro.common.MicroResponseCode;
import com.micro.common.util.JacksonUtil;
import com.micro.core.exception.BizException;
import com.micro.core.frame.service.impl.AbstractBasicsServiceImpl;
import com.micro.core.resp.RespJson;
import com.micro.core.resp.RespJsonData;
import com.micro.modual.entity.SystemRole;
import com.micro.modual.extension.SystemRoleExtension;
import com.micro.modual.mapper.SystemResourceAuthMapper;
import com.micro.modual.mapper.SystemRoleMapper;
import com.micro.modual.mapper.SystemUserRoleMapper;
import com.micro.modual.model.bo.systemrole.RoleStatusBO;
import com.micro.modual.model.bo.systemrole.SystemRoleBO;
import com.micro.modual.model.dto.systemrole.SystemRoleListDTO;
import com.micro.modual.service.SystemRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
*  <h1>系统角色表  实现</h1>
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020-08-04 00:11:10
 */
@Service("systemRoleService")
public class SystemRoleServiceImpl extends AbstractBasicsServiceImpl<SystemRole,Long> implements SystemRoleService {

    @Autowired
    private SystemRoleMapper systemRoleMapper;

    @Autowired
    private SystemUserRoleMapper systemUserRoleMapper;

    @Autowired
    private SystemResourceAuthMapper systemResourceAuthMapper;

    /**
     * <h2>新增系统角色表详情</h2>
     * @param systemRoleBO {@link SystemRoleBO} 系统角色表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson addSystemRole(SystemRoleBO systemRoleBO) {
        SystemRole systemRole = SystemRoleExtension.converAddSystemRole(systemRoleBO);
        systemRoleMapper.insertSelective(systemRole);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(), MicroResponseCode.SUCCESS.getResponseMessage());
    }

    /**
     * <h2>修改系统角色表详情</h2>
     * @param systemRoleBO  {@link SystemRoleBO} 系统角色表 新增/修改 请求对象
     * @return {@link RespJson} 返回结果基础类
     */
    @Override
    public RespJson editSystemRole(SystemRoleBO systemRoleBO) {
        SystemRole systemRole = SystemRoleExtension.converEditSystemRole(systemRoleBO);
        systemRoleMapper.updateByPrimaryKeySelective(systemRole);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

    /**
     * <h2>根据主键编号删除系统角色表信息</h2>
     * @param id  系统角色表 主键编号
     * @return  {@link RespJson} 返回结果基础类
     */

    @Override
    public RespJson deleteSystemRole(Long id) {
        SystemRole systemRole =  systemRoleMapper.selectByPrimaryKey(id);
        //验证角色是否分配用户
        if(systemUserRoleMapper.hasDistribution(systemRole.getRoleCode()) > 0){
            throw new BizException(MicroResponseCode.ROLE_HAS_DISTRIBUTION_USER.getResponseCode(),MicroResponseCode.ROLE_HAS_DISTRIBUTION_USER.getResponseMessage());
        }
        systemUserRoleMapper.deleteByRoleCode(systemRole.getRoleCode());
        systemResourceAuthMapper.deleteByRoleCode(systemRole.getRoleCode());
        systemRoleMapper.deleteByPrimaryKey(id);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJson validataRoleExist(String roleName) {
        if(systemRoleMapper.findRoleExist(roleName) > 0 ){
            return new RespJson(MicroResponseCode.ROLE_HAS_EXIST.getResponseCode(),MicroResponseCode.ROLE_HAS_EXIST.getResponseMessage());
        }else{
            return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
        }
    }

    @Override
    public RespJson modifyRoleStatus(RoleStatusBO roleStatusBO) {
        SystemRole systemRole =  systemRoleMapper.selectByPrimaryKey(roleStatusBO.getRoleId());
        //如果禁用角色
        if(roleStatusBO.getRoleStatus().equals(MicroConstant.StatusEffective.DISABLE)){
            if(systemUserRoleMapper.hasDistribution(systemRole.getRoleCode()) > 0){
                throw new BizException(MicroResponseCode.ROLE_HAS_DISTRIBUTION_USER.getResponseCode(),MicroResponseCode.ROLE_HAS_DISTRIBUTION_USER.getResponseMessage());
            }
        }
        systemRole.setRoleStatus(roleStatusBO.getRoleStatus());
        systemRoleMapper.updateByPrimaryKeySelective(systemRole);
        return new RespJson(MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());
    }

    @Override
    public RespJsonData<List<SystemRoleListDTO>> findRoleList() {
        List<SystemRole> systemRoleList = systemRoleMapper.findRoleList();
        List<SystemRoleListDTO> systemRoleListDTOList = JacksonUtil.getList(JacksonUtil.toJson(systemRoleList),SystemRoleListDTO.class);
        return new RespJsonData<List<SystemRoleListDTO>>(systemRoleListDTOList,MicroResponseCode.SUCCESS.getResponseCode(),MicroResponseCode.SUCCESS.getResponseMessage());

    }

}
